package com.wzh.frame.views;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wzh.frame.R;
import com.wzh.frame.utils.AppUtils;
import com.wzh.frame.utils.checkup.DownLoadManager;

import java.io.File;

/**
 * Created by Hu on 2016/10/26.
 */

public class FiexoDialog extends Dialog implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    public static final int NORMAL_TYPE = 0;
    public static final int ERROR_TYPE = 1;
    public static final int SUCCESS_TYPE = 2;
    public static final int WARNING_TYPE = 3;
    public static final int CUSTOM_IMAGE_TYPE = 4;
    public static final int PROGRESS_TYPE = 5;
    public static final int HWZ_EDIT = 6;
    public static final int HWZ_ListView = 7;
    public static final int CHECKUP_TYPE = 8;
    public static final int UPDATE_TYPE = 9;
    public static final int HTML_TYPE = 10;


    private int mAlertType;
    private TextView mTitleTextView, mContentTextView;
    private WebView webView;
    ProgressBar progressBar;
    FlikerProgressBar dowmProgress;
    Button mConfirmButton, mCancelButton;
    private ClearEditText clearEditText;
    private String mTitleText;
    private String mContentText;
    private String mCancelText;
    private String mConfirmText;

    private boolean mShowCancel;
    private boolean mShowmConfir;

    private boolean mShowContent;


    private FrameLayout checkLayout;
    private OnFiexoChangeListener mChangeListener;
    private TextView verNo, verSize, updateContent;
    private CheckBox overLook;
    String verNoText, verSizeText, updateConText,webUrl;

    OnFiexoClickListener mCancelClickListener, mConfirmClickListener;


    public FiexoDialog(Context context, int alertType) {
        super(context, R.style.alert_dialog);
        mAlertType = alertType;
    }

    public FiexoDialog(Context context, int alertType,String webUrl) {
        super(context, R.style.alert_dialog);
        mAlertType = alertType;
        this.webUrl=webUrl;
    }

    public FiexoDialog(Context context) {
        super(context, R.style.alert_dialog);
        mAlertType = 0;
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.cancel_button) {
            if (mCancelClickListener != null) {
                mCancelClickListener.onClick(FiexoDialog.this);
            } else {
//                dismissWithAnimation();
            }
        } else if (view.getId() == R.id.confirm_button) {
            if (mConfirmClickListener != null) {
                mConfirmClickListener.onClick(FiexoDialog.this);
            } else {
//                dismissWithAnimation();
            }
        }
    }

    public FiexoDialog setmChangeListener(OnFiexoChangeListener mChangeListener) {
        this.mChangeListener = mChangeListener;
        return this;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if (mChangeListener != null) {
            mChangeListener.onChecked(isChecked);
        }
    }

    public static interface OnFiexoChangeListener {
        void onChecked(boolean isChecked);
    }

    public static interface OnFiexoClickListener {
        public void onClick(FiexoDialog fiexoDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fiexo_dialog);
        initView();
        initEvent();
    }

    private void initEvent() {
        mConfirmButton.setOnClickListener(this);
        mCancelButton.setOnClickListener(this);

        overLook.setOnCheckedChangeListener(this);
    }

    public FiexoDialog setCancelClickListener(OnFiexoClickListener listener) {
        mCancelClickListener = listener;
        return this;
    }

    public FiexoDialog setConfirmClickListener(OnFiexoClickListener listener) {
        mConfirmClickListener = listener;
        return this;
    }

    private void initView() {
        mTitleTextView = (TextView) findViewById(R.id.title_text);
        mContentTextView = (TextView) findViewById(R.id.content_text);

        progressBar = (ProgressBar) findViewById(R.id.progress);
        dowmProgress = (FlikerProgressBar) findViewById(R.id.down_pregress);

        mConfirmButton = (Button) findViewById(R.id.confirm_button);
        mCancelButton = (Button) findViewById(R.id.cancel_button);


        checkLayout = (FrameLayout) findViewById(R.id.check_up_frame);
        verNo = (TextView) findViewById(R.id.new_ver_no);
        verSize = (TextView) findViewById(R.id.new_ver_size);
        updateContent = (TextView) findViewById(R.id.update_content);
        overLook = (CheckBox) findViewById(R.id.btn_overlook);

        webView = (WebView) findViewById(R.id.web_view);
        clearEditText = (ClearEditText) findViewById(R.id.input);
        setTitleText(mTitleText);
        setContentText(mContentText);
        setCancelText(mCancelText);
        setConfirmText(mConfirmText);


        setVerNo(verNoText);
        setVerSize(verSizeText);
        setUpdateContent(updateConText);


        changeAlertType(mAlertType, true);
    }

    private void changeAlertType(int alertType, boolean fromCreate) {
        mAlertType = alertType;
        switch (mAlertType) {
            case ERROR_TYPE:

                break;
            case SUCCESS_TYPE:

                break;
            case WARNING_TYPE:

                break;
            case CUSTOM_IMAGE_TYPE:

                break;
            case PROGRESS_TYPE:
                progressBar.setVisibility(View.VISIBLE);
                break;
            case HWZ_EDIT:
                clearEditText.setVisibility(View.VISIBLE);
                break;
            case HWZ_ListView:

                break;
            case CHECKUP_TYPE:
                checkLayout.setVisibility(View.VISIBLE);
                break;
            case UPDATE_TYPE:
                dowmProgress.setVisibility(View.VISIBLE);
                break;
            case HTML_TYPE:
                webView.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(webUrl))
                webView.loadUrl(webUrl);
                break;

        }

    }

    public FlikerProgressBar getDowmProgress() {
        return dowmProgress;
    }

    public FiexoDialog setConfirmText(String text) {
        mConfirmText = text;
        if (mConfirmButton != null && mConfirmText != null) {
            mConfirmButton.setText(mConfirmText);
            showConfirButton(true);
        }
        return this;
    }

    private FiexoDialog showConfirButton(boolean isShow) {
        mShowmConfir = isShow;
        if (mConfirmButton != null) {
            mConfirmButton.setVisibility(mShowmConfir ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    public FiexoDialog showCheckBox(boolean isShow) {
        mShowCancel = isShow;
        if (overLook != null) {
            overLook.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    public FiexoDialog setCancelText(String text) {
        mCancelText = text;
        if (mCancelButton != null && mCancelText != null) {
            showCancelButton(true);
            showCheckBox(true);
            mCancelButton.setText(mCancelText);
        }
        return this;
    }

    public FiexoDialog showCancelButton(boolean isShow) {
        mShowCancel = isShow;
        if (mCancelButton != null) {
            mCancelButton.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    public FiexoDialog setTitleText(String text) {
        mTitleText = text;
        if (mTitleTextView != null && mTitleText != null) {
            mTitleTextView.setText(mTitleText);
        }
        return this;
    }

    public FiexoDialog setContentText(String text) {
        mContentText = text;
        if (mContentTextView != null && mContentText != null) {
            showContentText(true);
            mContentTextView.setText(mContentText);
        }
        return this;
    }

    public FiexoDialog setMCancelable(boolean flag) {
        setCancelable(flag);
        return this;
    }

    public FiexoDialog showContentText(boolean isShow) {
        mShowContent = isShow;
        if (mContentTextView != null) {
            mContentTextView.setVisibility(mShowContent ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    public FiexoDialog setVerNo(String s) {
        verNoText = s;
        if (verNo != null && s != null) {
            verNo.setText("最新版本:" + s);
        }
        return this;
    }


    public FiexoDialog setVerSize(String s) {
        verSizeText = s;
        if (verSize != null && s != null) {
            verSize.setText("最新版本大小:" + s);
        }
        return this;
    }


    public FiexoDialog setUpdateContent(String s) {
        updateConText = s;
        if (updateContent != null && s != null) {
            String myString = s.replace(".", "。\n");
            updateContent.setText(myString);
        }
        return this;

    }

    public WebView getWebView() {
        return webView;
    }
}
